package com.buddies.lottery.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.MenuItem;

import com.blankj.utilcode.util.LogUtils;
import com.buddies.lottery.R;
import com.buddies.lottery.base.BaseActivity;
import com.buddies.lottery.ui.home.HomeFragment;
import com.buddies.lottery.ui.lottery.LotteryFragment;
import com.buddies.lottery.ui.user.UserFragment;
import com.buddies.lottery.utils.AppExitUtils;
import com.buddies.lottery.widget.OnTabClickListener;
import com.fm.openinstall.OpenInstall;
import com.fm.openinstall.listener.AppWakeUpAdapter;
import com.fm.openinstall.model.AppData;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {

    private ViewPager mPager;
    private BottomNavigationViewEx mNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mPager = (ViewPager) findViewById(R.id.pager);

        mNavigationView = (BottomNavigationViewEx) findViewById(R.id.navigation);
    }

    @Override
    protected void initData() {
        Beta.checkUpgrade();
        enterTab(getIntent());

        final List<Fragment> mItems = new ArrayList<>();
        mItems.add(new HomeFragment());
        mItems.add(new LotteryFragment());
        mItems.add(new UserFragment());

        mPager.setOffscreenPageLimit(mItems.size());
        mPager.setAdapter(new ViewPagerAdatper(getSupportFragmentManager(), mItems));

        mNavigationView.enableAnimation(false);
        mNavigationView.enableShiftingMode(false);
        mNavigationView.enableItemShiftingMode(false);
        mNavigationView.setupWithViewPager(mPager);
        mNavigationView.setOnNavigationItemReselectedListener(new BottomNavigationView.OnNavigationItemReselectedListener() {
            @Override
            public void onNavigationItemReselected(MenuItem item) {
                Fragment fragment = mItems.get(mPager.getCurrentItem());
                if (fragment instanceof OnTabClickListener) {
                    ((OnTabClickListener) fragment).onTabClick();
                }
            }
        });

        //获取唤醒参数
        OpenInstall.getWakeUp(getIntent(), wakeUpAdapter);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        enterTab(intent);

        // 此处要调用，否则App在后台运行时，会无法截获
        OpenInstall.getWakeUp(intent, wakeUpAdapter);
    }

    private void enterTab(Intent intent) {
        String tab = null;
        if (intent != null) {
            try {
                tab = intent.getStringExtra("tab");
            } catch (Exception e) {
                CrashReport.postCatchedException(e);
            }
        }

        int tabIndex = 0;
        if (!TextUtils.isEmpty(tab)) {
            if (tab.equalsIgnoreCase("home")) {
                tabIndex = 0;
            } else if (tab.equalsIgnoreCase("lottery")) {
                tabIndex = 1;
            } else if (tab.equalsIgnoreCase("user")) {
                tabIndex = 2;
            }
        }
        mPager.setCurrentItem(tabIndex);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    public int getCurrentTab() {
        return mPager.getCurrentItem();
    }

    @Override
    public void onBackPressed() {
        AppExitUtils.getInstance().onBackPressed(this);
    }

    private AppWakeUpAdapter wakeUpAdapter = new AppWakeUpAdapter() {
        @Override
        public void onWakeUp(AppData appData) {
            //获取渠道数据
            String channelCode = appData.getChannel();
            //获取绑定数据
            String bindData = appData.getData();
            LogUtils.d("OpenInstall", "getWakeUp : wakeupData = " + appData.toString());
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        wakeUpAdapter = null;
    }

    public static void launch(Context context) {
        launch(context, null);
    }

    public static void launch(Context context, String tab) {
        Intent intent = new Intent();
        intent.setClass(context, MainActivity.class);
        intent.putExtra("tab", tab);
        if (context instanceof MainActivity) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        }
        context.startActivity(intent);
    }

}
